<template>
  <div class="login_wrapper both-100" :style="bgImageUrl">
    <div class="login-video">
      <video :style="fixStyle" class="fillWidth" playsinline autoplay muted loop>
        <source src="@/assets/video/loginVideo.mp4" type="video/mp4" />
      </video>
    </div>
    <div class="login_box auto-margin">
      <el-form :model="loginForm" ref="form" class="login_form">
        <div class="login_hint">
          <h3 class="login_title">{{ systemTitle }}</h3>
        </div>
        <el-form-item prop="account">
          <el-input v-model="loginForm.username" placeholder="" autocomplete="off">
            <i slot="prefix" class="el-icon-info"></i>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" auto-complete="off" clearable placeholder="密码" show-password @keyup.enter.native="submitFrom('form')">
            <i slot="prefix" class="el-icon-warning"></i>
          </el-input>
        </el-form-item>
        <el-form-item prop="code" v-if="captchaEnabled" class="code">
          <el-input v-model="loginForm.code" auto-complete="off" placeholder="验证码" style="width: 66%" @keyup.enter.native="submitFrom('form')">
          </el-input>
          <div class="login_code">
            <template v-if="codeUrl">
              <img class="login_code__img" :src="codeUrl" @click="getCode" />
            </template>
          </div>
        </el-form-item>
        
        <el-form-item class="login_btn">
          <div class="login_point" type="primary" @click="submitFrom('form')" style="width: 100%">
            登录
          </div>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
import { getCodeImg } from '@/api'
import { encryptData } from '@/utils/encrypt'
export default {
  name: 'login',
  created () {
    this.getCode()
    this.getVideoStyle()
  },
  data () {
    return {
      loginForm: {
        username: '',
        password: '',
        rememberMe: false,
        code: '',
        uuid: '',
      },
      codeUrl: ``,
      captchaEnabled: true,
      bgImageUrl: {'background-image': `url(${require('@/assets/images/login_images/home_bg_blue.png')})`},
      systemTitle: '登录',
      models: {
        account: '',
        password: '',
      },
      rules: {
        account: [{ trigger: 'blur', required: true, message: '账号不能为空' }],

        password: [
          { trigger: 'blur', required: true, message: '密码不能为空' },
        ],
      },
      fixStyle: '',
      copyright:
        '©Copyrights 2015-2024 智慧指间科技有限公司 闽ICP备2021017017号-3',
    }
  },
  methods: {
    ...mapActions('user', ['getInfo', 'login']),
    // 获取验证码图片
    async getCode () {
      await getCodeImg().then((res) => {
        if (200 === res.code) {
          this.codeUrl = `data:image/gif;base64,${res.img}`
          this.loginForm.uuid = res.uuid
        }
      })
    },
    //登录
    submitFrom (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let { username, password, rememberMe, code, uuid } = this.loginForm
          password = encryptData(this.loginForm.password)
          const userInfo = {
            username,
            password,
            rememberMe,
            code,
            uuid
          }
          this.login(userInfo).then(res => {
            this.$router.push({
              path: '/dashboard'
            })
          }).catch(() => {
            this.getCode()
          })
        }
      })
    },
    resetPassword () {
      return this.loginForm.password = ''
    },
    getVideoStyle () {
      window.onresize = () => {
        const windowWidth = document.body.clientWidth
        const windowHeight = document.body.clientHeight
        const windowAspectRatio = windowHeight / windowWidth
        let videoWidth
        let videoHeight
        if (windowAspectRatio < 0.5625) {
          videoWidth = windowWidth
          videoHeight = videoWidth * 0.5625
          this.fixStyle = {
            height: windowWidth * 0.5625 + 'px',
            width: windowWidth + 'px',
            'margin-bottom': (windowHeight - videoHeight) / 2 + 'px',
            'margin-left': 'initial',
          }
        } else {
          videoHeight = windowHeight
          videoWidth = videoHeight / 0.5625
          this.fixStyle = {
            height: windowHeight + 'px',
            width: windowHeight / 0.5625 + 'px',
            'margin-left': (windowWidth - videoWidth) / 2 + 'px',
            'margin-bottom': 'initial',
          }
        }
      }
      window.onresize()
    },
  },
  computed: {},
}
</script>

<style lang="less">
@import url('@/common/elementui');
.login_wrapper {
  display: flex;
  align-items: center;
  background-color: #14161b;
  background-repeat: no-repeat;
  background-position: center;
  background-size: 100%;
  position: relative;
  .login_box {
    display: flex;
    align-items: center;
    width: 100%;
    position: relative;
    .login_form {
      width: 478px;
      padding: 25px 25px 5px 25px;
      position: absolute;
      right: 120px;
      box-sizing: border-box;
      border: 15px solid rgba(0, 0, 0, 0.5);
      background-color: #3d4043;
      .login_hint {
        background: url('@/assets/images/login_images/hint_bg.png') no-repeat center;
        line-height: 40px;
        margin-bottom: 30px;
        .login_title {
          font-size: 24px;
          color: #fff;
          font-weight: 500;
          text-align: center;
        }
      }
      .code {
        .el-form-item__content {
          display: flex;
          align-items: center;
          justify-content: space-between;
        }
        .login_code {
          width: 33%;
          height: 40px;
          cursor: pointer;
          img {
            width: 100%;
            height: 100%;
          }
        }
      }
      .el-input__icon {
        color: @primary;
      }
      .login_btn {
        cursor: pointer;
        background: url('@/assets//images/login_images/login_btn.png') no-repeat center;
        transition: all 0.3s ease;
        .login_point {
          font-weight: 500;
          font-size: 20px;
          text-align: center;
          color: #ffffff;
        }
      }
    }
  }
  .copyright {
    width: 100%;
    position: absolute;
    bottom: 10px;
    text-align: center;
    font-size: 12px;
    color: #ffffff;
    line-height: 20px;
  }
}
</style>
